package com.it.emall.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.it.emall.domain.Ingredients;
import com.it.emall.domain.Recipes;
import com.it.emall.service.RecipesService;
import com.it.emall.utils.ResponseUtils;
import com.it.emall.vo.IngredientsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 食谱controller
 */
@RestController
@RequestMapping("/admin/recipes")
public class RecipesController {

    @Autowired
    private RecipesService recipesService;

    /**
     * 列表
     * @param recipesId
     * @param name
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public Object list(Integer recipesId, String name,@RequestParam(value = "page",required = false,defaultValue = "1") Integer page,
                       @RequestParam(value = "limit",required = false,defaultValue = "10") Integer limit )
    {
        PageHelper.startPage(page,limit);

        List<Recipes> list = recipesService.selectRecipesList(recipesId, name);
        PageInfo<Recipes> pageInfo = new PageInfo<>(list);

        //组织data数据
        Map<String,Object> data = new HashMap<>();

        data.put("list",pageInfo.getList());
        data.put("total",pageInfo.getTotal());
        data.put("pages",pageInfo.getPages());
        data.put("page",pageInfo.getPageNum());
        data.put("limit",pageInfo.getPageSize());

        return ResponseUtils.ok(data);
    }
}
